/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.user;

import ui.admin.event.*;
import hibernate.dao.EventDAO;
import hibernate.dao.StaffDAO;
import hibernate.entity.Courses;
import hibernate.entity.Entities;
import hibernate.entity.EntityTypes;
import hibernate.entity.Staffs;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import ui.admin.AbstractAdminBean;
import ui.common.AbstractUIBean;
import util.Converter;

/**
 *
 * @author NDH
 */
@ManagedBean
@RequestScoped
public class UserEventManager extends AbstractAdminBean {

    EventDAO eDao = new EventDAO();
    String content;
    String title;
    boolean status = true;
    String startDateString, endDateString;
    Entities currEvent;
    Date startDate, endDate;
    private List<Entities> allEvent = new ArrayList<Entities>();
    List<Entities> upComing;

    public UserEventManager() {
        allEvent = eDao.readLiving();
        upComing = allEvent;
    }

    public List<Entities> getUpComing() {
        return upComing;
    }

    public void setUpComing(List<Entities> upComing) {
        this.upComing = upComing;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public boolean getStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public Entities getCurrEvent() {
        return currEvent;
    }

    public void setCurrEvent(Entities currEvent) {
        this.currEvent = currEvent;
    }

    public String getStartDateString() {
        return startDateString;
    }

    public void setStartDateString(String startDateString) {
        this.startDateString = startDateString;
    }

    public String getEndDateString() {
        return endDateString;
    }

    public void setEndDateString(String endDateString) {
        this.endDateString = endDateString;
    }

    /**
     * @param Entities fields in entity for modifying
     * @return String
     * @see edit page
     */
    public String edit(Entities c) {
        title = c.getTitle();
        content = c.getContent();
        startDate = c.getStartTime();
        endDate = c.getEndTime();
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        session.setAttribute("editEvent", c);
        return "edit";
    }

    /**
     * @param Courses a course for modifying startDate and endDate
     * @return void
     */
    void addDate(Courses c) {
        try {
            c.setStartDate(Converter.stringToDateJava(startDateString));
            c.setEndDate(Converter.stringToDateJava(endDateString));
        } catch (ParseException ex) {
            Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * @return String
     * @see listpage for displaying a collection of events in which an new event
     * has already been added
     */
    public String docreate() {
        currEvent = new Entities();
        EntityTypes eventType = eDao.readTypeByName("Event");
        currEvent.setEntityTypes(eventType);
        currEvent.setTitle(title);
        currEvent.setContent(content);
        currEvent.setStartTime(startDate);
        currEvent.setEndTime(endDate);
        currEvent.setStatus(true);
        if (eDao.create(currEvent) > 0) {
            showPrimeMessage("Success", "Event " + currEvent.getTitle() + " created");
            allEvent = eDao.readLiving();
            return "list?faces-redirect=true";
        }

        return null;
    }

    /**
     * @return String
     * @see a list page has the event updated successfully
     */
    public String doupdate() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        Entities e = (Entities) session.getAttribute("editEvent");
        e.setContent(content);
        e.setTitle(title);
        e.setStartTime(startDate);
        e.setEndTime(endDate);
        if (eDao.update(e) > 0) {
            showPrimeMessage("Success", "Event " + e.getTitle() + " updated");
            allEvent = eDao.readLiving();
            return "list?faces-redirect=true";
        }
        showPrimeMessage("Error", "Unexpected error! update fail");
        return null;
    }

    /**
     * @param Entities an entity chosen for removing
     * @return void
     * @see A list of event after removing this event
     */
    public void remove(Entities e) {
        if (eDao.remove(e) > 0) {
            showPrimeMessage("Success", "Event " + e.getTitle() + " has been removed");
            allEvent = eDao.readLiving();
        }
    }

    /**
     * @return List<Entities>
     * @see list page with all of events in the system
     */
    public List<Entities> getAllEvent() {
        return allEvent;
    }

    /**
     * @return List<Staffs>
     * @see list page with all of staffs
     */
    public List<Staffs> getAllStaffs() {
        return new StaffDAO().readAll();
    }
}
